#pragma once

#include <base/macro/bool.h>
#include <base/macro/byte.h>

/* 定义IF ELSE */
#define MIF(mcond)			__MIF(mcond)
#define MIF_ELSE(mcond, x, y)		_MIF_ELSE(TO_BOOL(mcond), x, y)
#define _MIF_ELSE(mcond, x, y)		__MIF_ELSE(mcond)(x, y)
#define __MIF(mcond)			__MIF_ELSE(TO_BOOL(mcond))

#define __MIF_ELSE(mcond)		__MCAT(__MIF_, mcond)	  //__MIF_##cond
#define __MIF_0(_x, y)			y
#define __MIF_1(x, _y)			x

#define WHEN(c)				MIF_ELSE(c, EXPAND, __DO_NOTHING)

#define REPEAT(count, macro, ...) \
	WHEN(count) \
	( \
		OBSTRUCT(__REPEAT) () \
		( \
			DEC8(count), macro, __VA_ARGS__ \
		) \
		OBSTRUCT(macro) \
		( \
			DEC8(count), __VA_ARGS__ \
		) \
	)

#define __REPEAT()			REPEAT

#define WHILE(pred, op, ...) \
	MIF_ELSE(pred(__VA_ARGS__)) \
	( \
		OBSTRUCT(__WHILE) () \
		( \
			pred, op, op(__VA_ARGS__) \
		), \
		__VA_ARGS__ \
	)
#define __WHILE()			WHILE


